package com.swufe.scoresforapp;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.NestedScrollingParent2;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.swufe.scoresforapp.Yonghu.Dataimport;
import com.swufe.scoresforapp.Yonghu.Dataoutput;
import com.swufe.scoresforapp.bean.SchoolBean;

import java.io.DataInput;
import java.util.ArrayList;
import java.util.List;

public class ScrollingMajorActivity extends AppCompatActivity {

    private TextView major_school;//开设该专业的学校
    private TextView major_school_place;//该学校所在地
    private TextView major_school_tel;//该学校招生电话
    private TextView major_school_class;//该学校类别
    private TextView major_school_2014;//2014年专业录取最低分
    private TextView major_school_2015;//2015年专业录取最低分
    private TextView major_school_2016;//2016年专业录取最低分
    private TextView major_school_2017;//2017年专业录取最低分
    private TextView major_school_rate;//录取概率
    private TextView studenttype;//考生类型
    List<SchoolBean> list = new ArrayList<>();
    private SchoolBean Major;
    private ImageView photo;//学校校徽
    private Handler handler;
    private String yonghu;
    private String y2014,y2015,y2016,y2017;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling_major);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what == 15){
                    major_school_2014.setText(y2014);
                    major_school_2015.setText(y2015);
                    major_school_2016.setText(y2016);
                    major_school_2017.setText(y2017);
                }if(msg.what == 13 ){
                    String result = (String)msg.obj;
                    Toast.makeText(ScrollingMajorActivity.this ,result, 0).show();
                }
                super.handleMessage(msg);
                super.handleMessage(msg);
            }
        };
        SharedPreferences preferences = getSharedPreferences("Data",Context.MODE_PRIVATE);
        yonghu = preferences.getString("Name","");
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Dataoutput out = new Dataoutput();
                        Dataimport in = new Dataimport();
                        Message msg = handler.obtainMessage(13);
                        if(in.getMShoucang(yonghu).contains(Major.major))
                            msg.obj ="已收藏该专业";
                        else {
                            msg.obj = out.setMajorShoucang(Major.Schoolname,yonghu,Major.major);
                        }
                        handler.sendMessage(msg);
                    }
                }).start();
            }
        });
        initData();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(Major.major);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void initData(){
        major_school = findViewById(R.id.major_school);
        major_school_place = findViewById(R.id.major_school_place);
        major_school_tel = findViewById(R.id.major_school_tel);
        major_school_class = findViewById(R.id.major_school_class);
        major_school_2014 = findViewById(R.id.major_school_2014);
        major_school_2015 = findViewById(R.id.major_school_2015);
        major_school_2016 = findViewById(R.id.major_school_2016);
        major_school_2017 = findViewById(R.id.major_school_2017);
        photo = findViewById(R.id.major_image);
        studenttype = findViewById(R.id.student_type);
        Intent intent = getIntent();
        String JsonData = intent.getStringExtra("information");
        Major = new Gson().fromJson(JsonData,SchoolBean.class);
        major_school.setText(Major.Schoolname);
        major_school_tel.setText(Major.phone);
        major_school_place.setText(Major.location.split(":")[1]);
        major_school_class.setText(Major.type.split(":")[1]);
        studenttype.setText(Major.studenttype);
        GlideImageLoader loader = new GlideImageLoader();
        loader.displayImage(getBaseContext(),Major.photosource,photo);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Dataimport in = new Dataimport();
                y2014 = in.getLow(Major.Schoolname,"2014",Major.major,Major.studentfrom,Major.studenttype);
                y2015 = in.getLow(Major.Schoolname,"2015",Major.major,Major.studentfrom,Major.studenttype);
                y2016 = in.getLow(Major.Schoolname,"2016",Major.major,Major.studentfrom,Major.studenttype);
                y2017 = in.getLow(Major.Schoolname,"2017",Major.major,Major.studentfrom,Major.studenttype);
                Message msg = handler.obtainMessage(15);
                handler.sendMessage(msg);
            }
        }).start();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
